package com.android.launcher3.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.telephony.SubscriptionInfo;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.x.component.connectivity.ConnectivityComponent;
import com.x.component.connectivity.ConnectivityTool;
import com.x.component.connectivity.IconState;
import com.x.component.connectivity.MobileDataIndicators;
import com.x.component.connectivity.MobileState;
import com.x.component.connectivity.WifiIndicators;
import com.x.component.connectivity.WifiState;

import java.util.List;

public class TopBar extends RelativeLayout {

    private String TAG = "TopBar";

    private ImageView topbarWifiIcon = null;
    private ImageView topbarCellar = null;
    private TextView tv_sim = null;

    private View layoutControlCenter;
    private View layoutSignal;

    public TopBar(Context context) {
        super(context);
        init();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setControlCenterClick(OnClickListener onClickListener) {
        layoutControlCenter.setOnClickListener(onClickListener);
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.top_bar, this, true);

        layoutControlCenter = findViewById(R.id.layout_control_center);
        layoutSignal = findViewById(R.id.layout_signal);
        topbarWifiIcon = findViewById(R.id.iv_wifi);
        topbarCellar = findViewById(R.id.iv_cellar);
        tv_sim = findViewById(R.id.tv_sim);
        ConnectivityComponent INSTANCE = ConnectivityComponent.getInstance();
        boolean simDetected = INSTANCE.isSimDetected();
        if (simDetected) {
            tv_sim.setVisibility(TextView.INVISIBLE);
        } else {
            tv_sim.setVisibility(TextView.VISIBLE);
        }

        screenAdaption();

        INSTANCE.setConnectivityTool(new ConnectivityTool() {
            @Override
            public void setWifiIndicators(WifiIndicators wifiIndicators) {
                Log.d(TAG, "setCallIndicator");
                WifiState wifiState = wifiIndicators.wifiState;
                if (wifiState != null) {
                    Log.d(TAG, "setWifiIndicators wifiState YYYY" + wifiState);


                    if (wifiState.enabled) {
                        topbarWifiIcon.setVisibility(TextView.VISIBLE);
                        int level = wifiState.level;
                        topbarWifiIcon.setImageLevel(level);
                    } else {
                        topbarWifiIcon.setVisibility(TextView.INVISIBLE);
                    }

                }
            }

            @Override
            public void setMobileDataIndicators(MobileDataIndicators mobileDataIndicators) {
                Log.d(TAG, "setMobileDataIndicators");
                MobileState mobileState = mobileDataIndicators.mobileState;
                if (mobileState != null) {
                    Log.d(TAG, "setMobileDataIndicators mobileState " + mobileState);
                    topbarCellar.setImageLevel(mobileState.level);
                }
            }

            @Override
            public void setSubs(List<SubscriptionInfo> list) {

            }

            @Override
            public void setNoSims(boolean show, boolean simDetected) {
                if (simDetected) {
                    topbarCellar.setImageResource(R.drawable.mobile_cellar_state);
                    tv_sim.setVisibility(TextView.INVISIBLE);
                } else {
                    topbarCellar.setImageResource(R.drawable.ic_no_singal);
                    tv_sim.setVisibility(TextView.VISIBLE);
                }
            }

            @Override
            public void setEthernetIndicators(IconState iconState) {

            }

            @Override
            public void setIsAirplaneMode(IconState iconState) {

            }

            @Override
            public void setMobileDataEnabled(boolean enabled) {

            }

            @Override
            public void setConnectivityStatus(boolean noDefaultNetwork, boolean noValidatedNetwork,
                                              boolean noNetworksAvailable) {

            }

            @Override
            public void setCallIndicator(IconState iconState, int subId) {
            }
        });

    }

    void screenAdaption() {


        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            layoutSignal.setBackgroundColor(Color.TRANSPARENT);
        }

        if (Utilities.isLongStripScreen(getContext())) {

            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
            RelativeLayout.LayoutParams layoutControlCenterLayoutParams = (LayoutParams) layoutControlCenter.getLayoutParams();
            layoutControlCenterLayoutParams.width = Utilities.pxFromDp(230, displayMetrics);
            layoutControlCenterLayoutParams.height = Utilities.pxFromDp(35, displayMetrics);
            layoutControlCenter.setLayoutParams(layoutControlCenterLayoutParams);

            RelativeLayout.LayoutParams layoutSignalLayoutParams = (LayoutParams) layoutSignal.getLayoutParams();
            layoutSignalLayoutParams.width = Utilities.pxFromDp(230, displayMetrics);
            layoutSignalLayoutParams.height = Utilities.pxFromDp(35, displayMetrics);
            layoutSignal.setLayoutParams(layoutSignalLayoutParams);
        }


    }


}
